package com.heima.wemedia.task;

import com.heima.common.exception.CustomException;
import com.heima.feign.client.ArticleFetignClient;
import com.heima.feign.client.TaskScheduleFeignClient;
import com.heima.model.article.dto.ArticleDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.enums.TaskTypeEnum;
import com.heima.model.schedule.entity.TaskInfo;
import com.heima.model.wemedia.entity.WmChannel;
import com.heima.model.wemedia.entity.WmNews;
import com.heima.model.wemedia.entity.WmUser;
import com.heima.utils.common.ProtostuffUtil;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.crypto.modes.kgcm.Tables4kKGCMMultiplier_128;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 文章自动发布任务
 */
@Slf4j
@Component
public class WmNewsAutoPublishTask {

    @Resource
    private WmChannelMapper wmChannelMapper;
    @Resource
    private WmUserMapper wmUserMapper;

    @Resource
    private TaskScheduleFeignClient taskScheduleFeignClient;

    @Resource
    private ArticleFetignClient articleFetignClient;

    @Resource
    private WmNewsMapper wmNewsMapper;



    /**
     * 新闻自动发布
     */
    @Scheduled(fixedDelay = 2000)
    public void wmNewsAutoPublish(){
        log.info("自动发布文章任务开始执行。。。。。。");
        while (true) {
            ResponseResult<TaskInfo> result = taskScheduleFeignClient.poll(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType(), TaskTypeEnum.NEWS_SCAN_TIME.getPriority());

            //拉取任务失败,或者数据为空 跳出循环
            if(result.getCode()!=200||result.getData()==null){
                break;
            }
            log.info("拉取任务成功,任务数据为:{}",result.getData());
            //获取任务参数
            TaskInfo taskInfo=result.getData();
            //反序列化数据
            WmNews param = ProtostuffUtil.deserialize(taskInfo.getParameters(), WmNews.class);


            //查询文章详情
            WmNews wmNews = wmNewsMapper.selectById(param.getId());
            log.info("查询到需要发布的文章：{}",wmNews);

            if(wmNews==null){
                continue;
            }
            //发布文章到APP端
            saveArticle(wmNews);
            log.info("文章成功发送到APP端。。。。。。。。。。。");
            //修改文章状态为审核通过已发布
            wmNews.setStatus(9);
            wmNewsMapper.updateById(wmNews);
            log.info("文章发布成功,修改文章状态为已发布");
        }


    }

   private void saveArticle(WmNews wmNews){
        ArticleDto articleDto = new ArticleDto();
        BeanUtils.copyProperties(wmNews,articleDto,"id");
        articleDto.setNewsId(wmNews.getId());

        //封装作者信息
        articleDto.setAuthorId(wmNews.getUserId());
        WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
        if(wmUser!=null){
            articleDto.setAuthorName(wmUser.getNickname());
        }
        //封装频道信息
        WmChannel wmChannel = wmChannelMapper.selectById(wmNews.getChannelId());
        if(wmChannel!=null){
            articleDto.setChannelName(wmChannel.getName());
        }
        articleDto.setLayout(wmNews.getType());
        ResponseResult<String> result = articleFetignClient.saveArticle(articleDto);
        if(result.getCode()!=200){
            throw new CustomException(AppHttpCodeEnum.SERVER_ERROR);
        }
    }





}
